package com.api.oa.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.controller.BaseController;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ElderlyDTO;
import com.api.shopapi.entity.dto.RetreatSettleDTO;
import com.api.shopapi.service.ElderlyService;
import com.api.shopapi.service.RetreatSettleService;
import com.api.shopapi.service.StaffService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 退住结算
 * */

@RequestMapping("RetreatSettle")
@RestController
public class RetreatSettleController extends BaseController {

    @Autowired
    @Qualifier("RetreatSettleServiceImplOne1")
    private RetreatSettleService retreatSettleService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;


    /**
     * 后台结算列表
     */
    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String elderlyName = jsonObject.getString("elderlyName");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/RetreatSettle/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);
            String institutionCode = staff.getInstitutionCode();

            PageInfo<RetreatSettle> retreatSettlePageInfo = retreatSettleService.getByInstitutionCode(pageIndex,pageSize,institutionCode,elderlyName);
            List<RetreatSettleDTO> retreatSettleDTOList = new ArrayList<>();
            for(RetreatSettle retreatSettle:retreatSettlePageInfo.getList()){
                RetreatSettleDTO retreatSettleDTO = mapper.map(retreatSettle,RetreatSettleDTO.class);
                retreatSettleDTOList.add(retreatSettleDTO);
            }
            for (RetreatSettleDTO retreatSettleDTO:retreatSettleDTOList){
                ElderlyDTO elderlyDTO = elderlyService.getByElderlyCode(retreatSettleDTO.getElderlyCode());
                retreatSettleDTO.setElderlyDTO(elderlyDTO);
            }
            retreatSettlePageInfo.setList(Collections.unmodifiableList(retreatSettleDTOList));

            return success(retreatSettlePageInfo);
        }  catch (Exception e) {
            insertError("/RetreatSettle/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    /**
     * 退住结算统计
     */
    @RequestMapping(value = "GetSumTotal", method = RequestMethod.POST)
    public MyResult GetSumTotal(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/RetreatSettle/GetSumTotal");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Staff staff = staffService.getByStaffCode(staffCode);
            String institutionCode = staff.getInstitutionCode();
            Object sumTotal = retreatSettleService.getSumTotalByInstitutionCode(institutionCode);
            if(sumTotal==null) {
                sumTotal = 0;
            }
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("sumTotal",sumTotal);
            return success(jsonObject1);
        }  catch (Exception e) {
            insertError("/RetreatSettle/GetSumTotal", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    /**
     * 客户端
     * 消费明细，结算列表
     */
    @RequestMapping(value = "GetConsumeByElderly", method = RequestMethod.POST)
    public MyResult GetConsumeByElderly(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            String elderlyCode = jsonObject.getString("elderlyCode");
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign, "/RetreatSettle/GetConsumeByElderly");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            List<RetreatSettle> list = retreatSettleService.getByElderlyCode(elderlyCode);
            return success(list);
        }  catch (Exception e) {
            insertError("/RetreatSettle/GetConsumeByElderly", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }



    //添加老人的结算记录
    @RequestMapping(value = "AddForOut",method = RequestMethod.POST)
    public MyResult AddForOut(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            if (!checkSignForOut(timeStr, sign)) {
                return failed("签名错误");
            }

            String elderlyCode = jsonObject.getString("elderlyCode");
            JSONArray listArr = jsonObject.getJSONArray("listArr");

            Elderly elderly = elderlyService.getByElderlyCode(elderlyCode);


            //增加新的关系
            for (int k = 0; k < listArr.size(); k++) {

                Date time = listArr.getJSONObject(k).getDate("time");
                double total = listArr.getJSONObject(k).getDouble("total");
                String remarks = listArr.getJSONObject(k).getString("remarks");

                RetreatSettle retreatSettle = new RetreatSettle();
                retreatSettle.setRetreatSettleCode(UUID.randomUUID().toString());
                retreatSettle.setElderlyCode(elderlyCode);
                retreatSettle.setElderlyName(elderly.getName());
                retreatSettle.setCreateTime(time);
                retreatSettle.setRemarks(remarks);
                retreatSettle.setTotal(total);
                retreatSettle.setInstitutionCode(elderly.getInstitutionCode());
                retreatSettleService.insert(retreatSettle);

            }

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/CommendCenterStaffRel/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }





}
